Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[
    ],
    zh: '',
    title:'',
    remark: '',
    webUrl: '',
    formData:{
    },
    result: ''
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })

    this.setData({
      apiType: options.zh,
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  },
  getFormData(e) {
  },
  verifyCopyFromChannel(audioBuffer, channelIndex, copiedArray, startInChannel = 0) {
    // 基本参数验证
    // if (!(audioBuffer instanceof AudioBuffer)) {
    //     return {
    //         success: false,
    //         message: '无效的AudioBuffer实例',
    //         details: null
    //     };
    // }

    if (typeof channelIndex !== 'number' || !Number.isInteger(channelIndex) || 
        channelIndex < 0 || channelIndex >= audioBuffer.numberOfChannels) {
        return {
            success: false,
            message: `无效的声道索引: ${channelIndex}`,
            details: null
        };
    }

    if (!(copiedArray instanceof Float32Array)) {
        return {
            success: false,
            message: '复制的数组必须是Float32Array实例',
            details: null
        };
    }

    if (startInChannel < 0 || startInChannel >= audioBuffer.length) {
        return {
            success: false,
            message: `无效的起始位置: ${startInChannel}`,
            details: null
        };
    }

    // 获取源数据
    const sourceData = audioBuffer.getChannelData(channelIndex);
    const maxCheckLength = Math.min(
        copiedArray.length, 
        sourceData.length - startInChannel
    );

    // 存储不匹配的位置信息
    const mismatches = [];
    const sampleTolerance = 1e-6; // 浮点数比较的容差

    // 逐样本比较
    for (let i = 0; i < maxCheckLength; i++) {
        const sourceValue = sourceData[startInChannel + i];
        const copiedValue = copiedArray[i];
        
        // 检查两个浮点数是否在容差范围内相等
        if (Math.abs(sourceValue - copiedValue) > sampleTolerance) {
            mismatches.push({
                position: i,
                sourceValue: sourceValue,
                copiedValue: copiedValue,
                difference: Math.abs(sourceValue - copiedValue)
            });

            // 限制最多记录10个不匹配项，避免性能问题
            if (mismatches.length >= 10) break;
        }
    }

    // 构建验证结果
    if (mismatches.length === 0) {
        return {
            success: true,
            message: `数据复制成功，共验证了 ${maxCheckLength} 个样本`,
            details: {
                checkedSamples: maxCheckLength,
                totalSamples: sourceData.length,
                startPosition: startInChannel,
                mismatches: 0
            }
        };
    } else {
        return {
            success: false,
            message: `数据复制失败，发现 ${mismatches.length} 处不匹配（最多显示10处）`,
            details: {
                checkedSamples: maxCheckLength,
                totalSamples: sourceData.length,
                startPosition: startInChannel,
                mismatches: mismatches.length,
                sampleMismatches: mismatches
            }
        };
    }
},
  choose1() {  
    const audioCtx = wx.createWebAudioContext()
    wx.request({
      url: 'https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/music/audio.mp3', // 音频 url
      responseType: 'arraybuffer',
      success: res => {
        audioCtx.decodeAudioData(res.data, audioBuffer => {
          const channels = audioBuffer.numberOfChannels;
          const frameCount = audioCtx.sampleRate * 2.0;
          const anotherArray = new Float32Array(frameCount);
          const rate = audioBuffer.sampleRate
          const startOffset = 0
          const endOffset = rate * 3;
          const newAudioBuffer = audioCtx.createBuffer(channels,endOffset - startOffset,rate)
          const offset = 0
          for (let channel = 0; channel < channels; channel++) {
           audioBuffer.copyFromChannel(anotherArray, channel, startOffset); 
          }
          const result1 = this.verifyCopyFromChannel(audioBuffer, 0, anotherArray);
          console.log('测试1（完整复制）:', result1);
          this.setData({
            result: result1
          })
        }, err => {
          console.error('decodeAudioData fail', err)
        })
      }
    })
  },
})